package com.example.week9_advanceui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileBrowse_Activity extends AppCompatActivity {

    ListView listView;
    TextView textView;
    File currentParent;
    File[]currentFiles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_browse_);


        listView=(ListView)findViewById(R.id.fileList);
        textView=(TextView)findViewById(R.id.filePath);

        File root= new File("/storage/");

        if(root.exists()){

            currentParent=root;
            currentFiles=root.listFiles();
            inflateListView(root.listFiles());
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?>adapterView, View view, int position, long id){

                if(currentFiles[position].isFile())
                    return;

                File[]tmp=currentFiles[position].listFiles();

                if(tmp==null||tmp.length==0){
                    Toast.makeText(FileBrowse_Activity.this,
                            "当前路径不可访问或该路径下没有文件",
                            Toast.LENGTH_LONG)
                            .show();
                }else{
                    currentParent=currentFiles[position];
                    currentFiles=tmp;
                    inflateListView(currentFiles);
                }
                Button parent=(Button)findViewById(R.id.fileParent);
                parent.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view){
                        try{
                            if(!currentParent.getCanonicalPath().equals("/storage/emulated/0")){
                                currentParent=currentParent.getParentFile();
                                currentFiles=currentParent.listFiles();
                                inflateListView(currentFiles);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }
    private void inflateListView(File[]files){
        List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
        for(int i=0;i<files.length;i++){

            Map<String,Object>listItem=new HashMap<>();
            if(files[i].isDirectory()){
                listItem.put("icon",R.drawable.folder);
            }else{
                listItem.put("icon",R.drawable.file);
            }
            listItem.put("fileName",files[i].getName());
            listItems.add(listItem);
        }

        SimpleAdapter simpleAdapter=new SimpleAdapter(this,
                listItems,
                R.layout.fileitem
                ,new String[]{"icon","fileName"},
                new int[]{R.id.fileIcon,R.id.file_name});
        listView.setAdapter(simpleAdapter);
        try{
            textView.setText("当前路径为："+currentParent.getCanonicalPath());
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}